<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template v-slot:message_param="{row}">

      <div class="el-form-item" v-for="(label,k) in row.param_config">
        <label class="el-form-item__label" style="width: 8em;">{{label}}</label>
        <div class="el-form-item__content">
          <input type="hidden" :name="row.name+'['+k+']'" :value="row.value[k]">
          <div class="flex" style="width: 100%;">
            <text_one class="max" v-model:value="row.value[k]" computed="1"></text_one>
          </div>
        </div>
      </div>
    </template>


  </forms>
</template>

<script>
import text_one from "@/components/auto_import/UnifyParam/text_one.vue";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";
import {iframe} from "@/js/tool.js";
export default {
  components: {text_one},
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let forms_config = {
      model_name: {label:'模型',name:'model_name',type:'select_one',config:[],tips:'',value: ''},
      event_name: {label:'触发事件',name:'event_name',type:'select_one',config:[],tips:''},
      throw: {label:'抛出异常',name:'throw',type:'radio_switch',tips:'如果抛出移除，执行方法出错时，会中断程序',value:0},
      function: {label:'执行方法',name:'function',type:'select_one',config:[],tips:''},
    }

    let model_map={}, all_action_map={}, function_map={}, field_map={};
    ajax({url:'/admin/model_v2_event/config_map',cache:true,async:false},function (res){
      function_map = res.data.function_map;
      field_map = res.data.field_map;

      model_map = res.data.model_map;
      all_action_map = res.data.all_action_map;

      forms_config.model_name.config = model_map;
      forms_config.function.config = [];
      Object.keys(function_map).forEach((k)=>{
        forms_config.function.config.push({content:function_map[k].content, value:k});
      })

      Object.keys(field_map).forEach((k)=>{
        forms_config[k] = field_map[k];
        forms_config[k].remove = true;
      })
      return false;
    })

    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
      function_map: function_map,
      field_map: field_map,

      model_map: model_map,
      all_action_map: all_action_map,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/model_v2_event/edit?id=`+this.id : '/admin/model_v2_event/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('model_v2_event/index', '$refs.list_url.list');
          iframe.hide('model_v2_event/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
    change_function(){
      let app = this;
      let func = this.config.function.value;
      let params = !func || !app.function_map[func] ? [] : app.function_map[func].param;
      s94Mjs.each(app.field_map, function(v,k){
        if (~params.indexOf(k)){
          app.config[k].remove = false;
        }else {
          app.config[k].remove = true;
        }
      })
    },
    change_message_id(id, config_name){
      let app = this;
      let old_v = app.config[config_name].value || {};

      ajax('/admin/message_template/info?id='+id, function(res){
        let param_map = JSON.parse(res.data.param);
        app.config[config_name].value = {};
        Object.keys(param_map).forEach((k)=>{
          app.config[config_name].value[k] = old_v[k] || '';
        })
        app.config[config_name].param_config = param_map;
        return false;
      })
    },
  },
  watch:{
    'config.function.value'(func){
      let app = this;
      this.change_function();
    },
    'config.p30.value'(v, oldV){
      if (v && v != oldV) this.change_message_id(v, "p31");
    },
    'config.model_name.value'(v, oldV){
      let action_map = this.all_action_map[v] || {}
      this.config.event_name.config = action_map;
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/model_v2_event/info?id='+this.id, cache:true}, (res)=>{
        let param = JSON.parse(res.data.param);
        delete res.data.param;
        app.$refs['forms'].init(res.data);
        setTimeout(function(){
          s94Mjs.each(app.field_map, function(row,k){
            if (app.config[k].remove) return;
            let ms = app.config[k].name.match(/param\[(\d+)\]/);
            if (ms && param[ms[1]]){
              app.config[k].value = param[ms[1]];
            }
          })
        },10)
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('model_v2_event/edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
  }
}
</script>
